package main

import (
	"context"
	"fmt"
	"github.com/go-kit/kit/endpoint"
	"github.com/go-kit/kit/log"
	"github.com/go-kit/kit/sd"
	"github.com/go-kit/kit/sd/consul"
	httptransport "github.com/go-kit/kit/transport/http"
	consulapi "github.com/hashicorp/consul/api"
	"io"
	"net/url"
	"os"
	"sz_client/Endpoint"
	"sz_client/Transport"
)

func main2() {
	tgt,_ := url.Parse("http://localhost:8080")
	client := httptransport.NewClient("GET", tgt, Transport.GetUserInfo_Request, Transport.GetUserInfo_Response)
	getUserInfo := client.Endpoint()

	ctx := context.Background()
	res,err := getUserInfo(ctx, Endpoint.Request{Uid: 211})
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	userinfo := res.(Endpoint.Response)
	fmt.Println(userinfo.Result)
}

func main() {
	{
		config := consulapi.DefaultConfig()
		config.Address = "http://localhost:8500"
		api_client, _ := consulapi.NewClient(config)
		client := consul.NewClient(api_client)

		var logger log.Logger
		{
			logger = log.NewLogfmtLogger(os.Stdout)
		}
		{
			tags := []string{"primary"}
			instancer := consul.NewInstancer(client, logger, "userservice",tags,true)
			{
				factory := func(service_url string) (endpoint.Endpoint, io.Closer, error) {
					tart,_ := url.Parse("http://"+service_url)
					return httptransport.NewClient("GET",tart,Transport.GetUserInfo_Request, Transport.GetUserInfo_Response).Endpoint(),nil,nil

				}
				enpointer := sd.NewEndpointer(instancer,factory,logger)
				endpoints,_ := enpointer.Endpoints()
				fmt.Println("有",len(endpoints),"条服务")
				getUserInfo := endpoints[0]


				ctx := context.Background()
				res,err := getUserInfo(ctx, Endpoint.Request{Uid: 211})
				if err != nil {
					fmt.Println(err)
					os.Exit(1)
				}
				userinfo := res.(Endpoint.Response)
				fmt.Println(userinfo.Result)
			}
		}
	}
}
